package ciyuan.Block.Machine.Simple.Anvil;

import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import ciyuan.Block.TypeInterface.MachineTileEntity;
import ciyuan.Item.ItemRegisterManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class AnvilTileEntity extends MachineTileEntity {

    public AnvilTileEntity() {
        super(SimpleMachineRegister.SMELTER_TILE_ENTITY.get(), 3, new TranslationTextComponent("ciyuan.container.dsc17"), 0);
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new AnvilContainer(SimpleMachineRegister.ANVIL_CONTAINER.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void tick() {
        if (!this.getInventory().getItem(0).isEmpty() && !this.getInventory().getItem(1).isEmpty()) {
            if (this.getInventory().getItem(0).getItem().getDescriptionId().equals(ItemRegisterManager.forgehammer.get().getDescriptionId())) {
                Item item = null;
                String itemName = this.getInventory().getItem(1).getItem().getDescriptionId();
                if (itemName.equals(Items.IRON_INGOT.getDescriptionId())) {
                    item = ItemRegisterManager.ironplate.get();
                } else if (itemName.equals(ItemRegisterManager.Steel.get().getDescriptionId())) {
                    item = ItemRegisterManager.steelplate.get();
                } else if (itemName.equals(ItemRegisterManager.Tin_Alloy.get().getDescriptionId())) {
                    item = ItemRegisterManager.tinAlloyPlate.get();
                }

                if (item != null && this.getInventory().canAddItem(new ItemStack(item))) {
                    if (this.getInventory().getItem(0).getDamageValue() >= this.getInventory().getItem(0).getMaxDamage()) {
                        this.getInventory().setItem(0, new ItemStack(null));
                    } else {
                        this.getInventory().getItem(0).setDamageValue(this.getInventory().getItem(0).getDamageValue() + 1);
                    }
                    this.getInventory().getItem(1).setCount(this.getInventory().getItem(1).getCount() - 1);
                    this.getInventory().addItem(new ItemStack(item));
                }
            }
        }
    }

}
